﻿using System.Collections.Generic;
using System.Linq;
using Myotragus.Data.Containers;
using Myotragus.Data.Emit;
using Myotragus.Data.Hbm;
using Myotragus.Data.Util;

namespace Myotragus.Data.Registration
{
	public static class Generators
	{
		public static IEntitiesImplementationCollection GenerateAutomaticImplementation(
			this IEntitiesInterfacesCollection @this, IServicesContainer serviceContainer)
		{
			var generator = serviceContainer.Resolve<IAutoProperties>();
			return new EntitiesImplementationCollection
			{
				Actual = @this.Select(t => generator.GetImplementation(t))
			};
		}

		public static IEnumerable<string> GenerateAutomaticHbm(
			this IEntitiesInterfacesCollection @this, IServicesContainer serviceContainer)
		{
			var generator = serviceContainer.Resolve<IAutomaticHbmGenerator>();
			var entities = serviceContainer.Resolve<IEntitiesContainer>();

			return from type in @this
					 let impl = entities.Create(type).GetType()
					 select generator.GenerateMapping(type);
		}

		public static IEnumerable<string> GenerateAutomaticHbm(
			this IEntitiesImplementationCollection @this, IServicesContainer serviceContainer)
		{
			var generator = serviceContainer.Resolve<IAutomaticHbmGenerator>();

			return from type in @this
					 let intr = ReflectionHelper.GetDefaultInterface(type)
					 select generator.GenerateMapping(intr);
		}
	}
}